import Vue from 'vue'
import VueRouter from 'vue-router'
import authArr from "@/router/authroute.js"

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    name: "index",
    redirect: "/login"
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../components/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    // redirect: "/home/edmange",
    component: () => import(/* webpackChunkName: "about" */ '../views/Home.vue'),
    children: [
      {
        path: 'edmange',
        name: "代理商管理",
        component: () => import(/* webpackChunkName: "about" */ '../components/edmange.vue'),
      },
      {
        path: 'edreport',
        name: "代理商报告",
        component: () => import(/* webpackChunkName: "about" */ '../components/edreport.vue'),
      },
      {
        path: 'edrequest',
        name: "代理商请求",
        component: () => import(/* webpackChunkName: "about" */ '../components/edrequest.vue'),
      },
      {
        path: 'coins',
        component: () => import(/* webpackChunkName: "about" */ '../views/coins.vue'),
      },
      {
        path: 'adcharge',
        name: "用户充值",
        component: () => import(/* webpackChunkName: "about" */ '../components/adcharge.vue'),
      },
      {
        path: 'adrecoin',
        name: "用户充值记录",
        component: () => import(/* webpackChunkName: "about" */ '../components/adrecoin.vue'),
      },
      {
        path: 'adrecord',
        name: "用户代理请求记录",
        component: () => import(/* webpackChunkName: "about" */ '../components/adrecord.vue'),
      },
      {
        path: 'adbuy',
        name: "用户代理购买",
        component: () => import(/* webpackChunkName: "about" */ '../components/adbuy.vue'),
      },
      {
        path: 'admanage',
        name: "用户修改密码",
        component: () => import(/* webpackChunkName: "about" */ '../components/adchangepw.vue'),
      }
    ]
  },




]

const router = new VueRouter({

  base: process.env.BASE_URL,
  routes
})

//全局前置导航守卫
router.beforeEach((to, from, next) => {
  if (authArr.indexOf(to.path) !== -1) {
    let token = localStorage.getItem("token")
    if (token) {
      // alert("1")
      next()
    } else {
      next("/")
    }
    // next()
  } else {
    next()
  }

})


export default router
